import { defineStore } from 'pinia'
import { 
  createMedia, updateMedia, deleteMedia, 
  deleteMediaByNoteId, getMediaById, 
  getMediaByNoteId, getMediaByType, 
  updateMediaOrder 
} from '@/api/media'

export const useMediaStore = defineStore('media', {
  state: () => ({
    mediaList: [],
    currentMedia: null,
    isLoading: false,
    error: null
  }),
  actions: {
    async createMedia(mediaData) {
      this.isLoading = true
      this.error = null
      try {
        const newMedia = await createMedia(mediaData)
        this.mediaList.push(newMedia)
        return newMedia
      } catch (error) {
        this.error = error.message || '创建媒体失败'
        return null
      } finally {
        this.isLoading = false
      }
    },
    
    async updateMedia(id, mediaData) {
      this.isLoading = true
      this.error = null
      try {
        const updatedMedia = await updateMedia(id, mediaData)
        const index = this.mediaList.findIndex(media => media.id === id)
        if (index !== -1) {
          this.mediaList.splice(index, 1, updatedMedia)
        }
        if (this.currentMedia && this.currentMedia.id === id) {
          this.currentMedia = updatedMedia
        }
        return updatedMedia
      } catch (error) {
        this.error = error.message || '更新媒体失败'
        return null
      } finally {
        this.isLoading = false
      }
    },
    
    async deleteMedia(id) {
      this.isLoading = true
      this.error = null
      try {
        await deleteMedia(id)
        this.mediaList = this.mediaList.filter(media => media.id !== id)
        if (this.currentMedia && this.currentMedia.id === id) {
          this.currentMedia = null
        }
        return true
      } catch (error) {
        this.error = error.message || '删除媒体失败'
        return false
      } finally {
        this.isLoading = false
      }
    },
    
    async deleteMediaByNoteId(noteId) {
      this.isLoading = true
      this.error = null
      try {
        await deleteMediaByNoteId(noteId)
        this.mediaList = this.mediaList.filter(media => media.noteId !== noteId)
        return true
      } catch (error) {
        this.error = error.message || '删除笔记媒体失败'
        return false
      } finally {
        this.isLoading = false
      }
    },
    
    async fetchMediaById(id) {
      this.isLoading = true
      this.error = null
      try {
        const media = await getMediaById(id)
        this.currentMedia = media
        return media
      } catch (error) {
        this.error = error.message || '获取媒体失败'
        return null
      } finally {
        this.isLoading = false
      }
    },
    
    async fetchMediaByNoteId(noteId) {
      this.isLoading = true
      this.error = null
      try {
        const mediaList = await getMediaByNoteId(noteId)
        this.mediaList = mediaList
        return mediaList
      } catch (error) {
        this.error = error.message || '获取笔记媒体失败'
        return []
      } finally {
        this.isLoading = false
      }
    },
    
    async fetchMediaByType(mediaType) {
      this.isLoading = true
      this.error = null
      try {
        const mediaList = await getMediaByType(mediaType)
        this.mediaList = mediaList
        return mediaList
      } catch (error) {
        this.error = error.message || '获取媒体列表失败'
        return []
      } finally {
        this.isLoading = false
      }
    },
    
    async updateMediaOrder(id, orderIndex) {
      this.isLoading = true
      this.error = null
      try {
        const updatedMedia = await updateMediaOrder(id, orderIndex)
        const index = this.mediaList.findIndex(media => media.id === id)
        if (index !== -1) {
          this.mediaList[index].orderIndex = orderIndex
        }
        if (this.currentMedia && this.currentMedia.id === id) {
          this.currentMedia.orderIndex = orderIndex
        }
        return updatedMedia
      } catch (error) {
        this.error = error.message || '更新媒体顺序失败'
        return null
      } finally {
        this.isLoading = false
      }
    }
  }
})